package com.ray.depot.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Dict;
import com.ray.depot.service.WarehouseTypeService;
import com.ray.depot.table.entity.WarehouseType;
import com.ray.depot.util.DataGridUtil;
import com.ray.depot.util.DictLoadUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

@Controller
@RequestMapping("/depot/type")
public class WarehouseTypeController extends WebController {

	@Autowired
	UcenterApiService ucenterApiService;
	@Autowired
	WarehouseTypeService warehouseTypeService;

	@RequestMapping(value = "view")
	@RequiresPermissions("depot:type:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		modelAndView.setViewName("depot/type/view");
		return modelAndView;
	}

	/**
	 * 新增页面
	 * 
	 * @param menuId
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("depot:type:add")
	public ModelAndView addView(String menuId, WarehouseType record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(ucenterApiService, "useStatus");
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("record", record);
		modelAndView.setViewName("depot/type/add");
		return modelAndView;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("depot:type:add")
	@ResponseBody
	public ResponseEntity<Result<Object>> add(WarehouseType params, HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		Result<Object> result = warehouseTypeService.addWarehouseType(params, user);
		return doResult(result);
	}

	/*
	 * 修改页面
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("depot:type:edit")
	@ResponseBody
	public ModelAndView editView(String menuId, String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		WarehouseType record = warehouseTypeService.queryWarehouseType(id, user);
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(ucenterApiService, "useStatus");
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("record", record);
		modelAndView.setViewName("depot/type/edit");
		return modelAndView;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "edit")
	@RequiresPermissions("depot:type:edit")
	@ResponseBody
	public ResponseEntity<Result<Object>> update(WarehouseType params, HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		Result<Object> result = warehouseTypeService.updateWarehouseType(params, user);
		return doResult(result);

	}

	/**
	 * 详情页面
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("depot:type:detail")
	@ResponseBody
	public ModelAndView detailView(String menuId, String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		WarehouseType record = warehouseTypeService.queryWarehouseType(id, user);
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(ucenterApiService, "useStatus");
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("params", record);
		modelAndView.setViewName("depot/type/detail");
		return modelAndView;

	}

	/**
	 * 删除页面
	 */
	@RequestMapping(value = "delete")
	@RequiresPermissions("depot:type:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> delete(String id, HttpServletRequest request, HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		Result<Object> result = warehouseTypeService.deleteWarehouseType(id, user);
		return doSuccess(result);

	}

	/**
	 * 用户查询页面
	 */
	@RequestMapping(value = "query")
	@RequiresPermissions("depot:type:query")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> query(WarehouseType params, HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		PageDto<WarehouseType> page = this.createJQPage(request);
		page = warehouseTypeService.queryWarehouseType(params, page, user);
		return doSuccess(DataGridUtil.dataConvert(page));

	}

}
